package cn.flying.cloud.message.controller;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.message.bo.MsgStorageBo;
import cn.flying.cloud.message.core.service.MessageService;
import cn.flying.cloud.message.model.MessageRequest;
import cn.flying.cloud.message.service.MsgStorageService;

/**
 * 消息配置信息表 前端控制器
 *
 * @author: admin
 * @date: 2025-09-24 10:42:07
 * @version: 1.0
 */
@RestController
@RequestMapping("/message/storage")
public class MsgStorageController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private MsgStorageService msgStorageService;
    @Resource
    private MessageService messageService;

    /**
     * 分页查询消息配置信息表数据
     *
     * @param request 参数
     * @return
     */
    @PostMapping("page")
    public PageResponse<MsgStorageBo> page(@RequestBody PageRequest<MsgStorageBo> request) {
        return msgStorageService.getPageList(request);
    }

    /**
     * 列表查询消息配置信息表数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody MsgStorageBo param) {
        return Rt.success(msgStorageService.getList(param));
    }

    /**
     * 查询消息配置信息表详情数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(MsgStorageBo param) {
        return Rt.success(msgStorageService.getOneObj(param));
    }

    /**
     * 根据主键ID查询消息配置信息表详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        return Rt.success(msgStorageService.getObjById(id));
    }

    /**
     * 保存消息配置信息表数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody MsgStorageBo param) {
        if (Objects.isNull(param.getId())) {
            // 新增
            msgStorageService.createObj(param);
        } else {
            // 更新
            msgStorageService.updateObj(param);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除消息配置信息表数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(MsgStorageBo param) {
        msgStorageService.deleteObj(param);
        return Rt.success("删除成功！");
    }

    /**
     * 根据主键ID删除消息配置信息表数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        msgStorageService.deleteById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 测试配置的是否有效
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("send")
    public Rt<?> send(Long id, String tplId, String to) {
        MsgStorageBo storage = msgStorageService.getObjById(id);
        MessageRequest request = new MessageRequest();
        request.setTenantId(Ft.Local.TenantId());
        request.setRequestNo(UUID.randomUUID().toString());
        request.setServiceCode("bridge");
        List<String> list = new ArrayList<String>() {{
            add(to);
        }};
        request.setRecipients(list);
        request.setTitle("测试");
        request.setContent("这是一条测试消息！");
        request.setTplId(tplId);
        request.setParam(new HashMap<>());
        request.setFilePaths(new ArrayList<>());

        if ("S".equals(storage.getMsgType())) {
            messageService.sendSms(request);
        } else {
            messageService.sendSimpleMail(request);
        }
        return Rt.success("发送成功！");
    }
}
